package com.cungmua.app.task;

import android.app.ProgressDialog;
import android.content.Context;

import com.cungmua.app.handler.BaseRequestHandler;
import com.cungmua.app.handler.LoginHandler;
import com.cungmua.app.model.request.LoginRequest;
import com.cungmua.app.model.response.LoginResponse;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 6/23/14.
 */
public class LoginTask extends BaseRequestTask<LoginRequest, Void, LoginResponse> {
    private final LoginHandler handler;
    private final Context context;
    private ProgressDialog dialog;

    public LoginTask(Context context, LoginHandler handler) {
        this.handler = handler;
        this.context = context;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected LoginResponse doInBackground() throws RetrofitError {
        LoginRequest request = params[0];

        return service.login(request.toFieldMap());
    }

    @Override
    protected BaseRequestHandler getHandler() {
        return handler;
    }


    public void setParams(LoginRequest... params) {
        this.params = params;
    }

    @Override
    protected void onSuccess(LoginResponse response) {
        if (response.isLoginSuccess())
            handler.loginSuccess(response);
        else
            handler.loginFailed(response);
    }
}
